package admin;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.objective.oji.OjiException;
import com.objective.oji.OjiObject;
import com.objective.oji.OjiSearch;
import com.objective.oji.OjiSession;
import com.objective.oji.OjiTypeDefn;


public class Search_Builder {

    static List<Search_Condition> search_condition_list = new ArrayList<Search_Condition>();
    OjiSearch newSearch;
    OjiSession apiSession;
    OjiTypeDefn search_type;
    Search_Condition search_condition;
    int Max_Returns_Count;

    public Search_Builder(OjiSession myApiSession,int Max_Returns)
    {
	apiSession = myApiSession;
	newSearch = apiSession.initAdvancedSearch();
	search_condition = new Search_Condition();
	Max_Returns_Count = Max_Returns;
    }

    public String get_Search_type()
    {
	return search_type.getSingularName();
    }

    public  void set_Search_type(String object_type) throws OjiException
    {
	//OjiTypeDefn search_type = apiSession.getTypeDefnByName("document");
	//OjiTypeDefn search_type = apiSession.getTypeDefnByName("duotA2, duotA15, duotA16");

	search_type = apiSession.getTypeDefnByName(object_type);
	newSearch.setObjectSearchType(search_type);
    }

    public void addCriteria (String catalogue, String operator, String filter)
    {
	try {
	    newSearch.addCriteria(catalogue, operator, filter, 0, 0,OjiSearch.AND_RELATION);
	} catch (OjiException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }

    public Collection<OjiObject>  execute ()
    {
	try {
	    newSearch.setMaximumResults(Max_Returns_Count);
	    newSearch.execute();

	} catch (OjiException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	@SuppressWarnings("unchecked")
	Collection<OjiObject> results = (Collection<OjiObject>) newSearch.getResults();
	return results;
    }
}

